{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[37.2, 37.6, 36.8]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "'''\n",
    "作业一： \n",
    "复习矩阵相关操作，并完成下题: 菜价 \n",
    "上面的数据，可以在numpy中表示如下 5分 \n",
    "X = np.array([[1.2, 1.5, 1.8], \n",
    "[1.3, 1.4, 1.9], \n",
    "[1.1, 1.6, 1.7]]) \n",
    "y = np.array([5, 10, 9]).T \n",
    "1. 使用循环的方式计算每天的采购总金额 得到结果为[37.2, 37.6, 36.8]，分别表示7/28、7/29、7/30这三天采购总额 10分 \n",
    "2. 使用矩阵点乘来计算每天的采购总金额（使用np.dot来实现矩阵相乘） 5分 \n",
    "3. 测试两种方式的性能 10分 \n",
    "\n",
    "'''\n",
    "X = np.array([[1.2, 1.5, 1.8], [1.3, 1.4, 1.9], [1.1, 1.6, 1.7]]) \n",
    "Y = np.array([5, 10, 9]).T \n",
    "#使用循环的方式计算每天的采购总金额\n",
    "loop_result=[0,0,0]\n",
    "def sum_loop():\n",
    "    for index,val in enumerate(x):\n",
    "        loop_result[index]=round(val[0]*Y[0]+val[1]*Y[1]+val[2]*Y[2],1)\n",
    "sum_loop()\n",
    "print(loop_result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[37.2, 37.6, 36.8]\n"
     ]
    }
   ],
   "source": [
    "#2.使用矩阵点乘来计算每天的采购总金额\n",
    "np.result=[0,0,0]\n",
    "def sum_np():\n",
    "    np.result=[round(np.dot(valx,Y),1) for valx in X]\n",
    "sum_np()\n",
    "print(np.result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "49.3 µs ± 1.2 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit sum_loop()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 154,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "30.4 µs ± 2.28 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit sum_np()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9, 6],\n",
       "       [1, 1, 2],\n",
       "       [8, 7, 3],\n",
       "       [5, 6, 3],\n",
       "       [5, 3, 5],\n",
       "       [8, 8, 2],\n",
       "       [8, 1, 7],\n",
       "       [8, 7, 2],\n",
       "       [1, 2, 9],\n",
       "       [9, 4, 9]])"
      ]
     },
     "execution_count": 155,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'''\n",
    "\n",
    "作业二： \n",
    "阅读下面的代码 \n",
    "np.random.seed(1) \n",
    "X = np.random.randint(1, 10, size=30) \n",
    "此时X中的数据如下: \n",
    "array([6, 9, 6, 1, 1, 2, 8, 7, 3, 5, 6, 3, 5, 3, 5, 8, 8, 2, 8, 1, 7, 8,7, 2, 1, 2 , 9, 9, 4, 9]) \n",
    "请将X处理为一个3列的矩阵，如下: 5分 \n",
    "------------\n",
    "假设这是10条样本数据，前两列是样本的两个特征，第3列是样本的分类标记，请分离出样本的特征和分类 标记，分别存放在两个变量中，用 X_train 存放样本特征(红色部份), y_train 存放分类标记(绿色部份) 5分 \n",
    "请用numpy的比较运算，通过 y_train 中的数据，分离出 X_train 中的3个分类，如下图 5分 \n",
    "'''\n",
    "#请将X处理为一个3列的矩阵，如下: 5分 \n",
    "np.random.seed(1) \n",
    "X = np.random.randint(1, 10, size=30) \n",
    "Y=X.reshape(K=Y[:,2:3]10,3)\n",
    "Y\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 160,
   "metadata": {},
   "outputs": [],
   "source": [
    "K=Y[:,2:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 159,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0],\n",
       "       [1],\n",
       "       [0],\n",
       "       [2],\n",
       "       [0],\n",
       "       [2],\n",
       "       [2]])"
      ]
     },
     "execution_count": 159,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#将第三列中，小于等于3的修改为0、大于3且小于等于6的修改为1、大于6的修改为2，结果如下: 10分 \n",
    "#K=np.zeros(shape=(1,10))\n",
    "K=Y[:,2:3]\n",
    "for i in range(10):\n",
    "    if K[i][0]>6:\n",
    "        K[i][0]=2\n",
    "    elif K[i][0]<=3:\n",
    "        K[i][0]=0\n",
    "    else:\n",
    "        K[i][0]=1\n",
    "K\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9, 1],\n",
       "       [1, 1, 0],\n",
       "       [8, 7, 0],\n",
       "       [5, 6, 0],\n",
       "       [5, 3, 1],\n",
       "       [8, 8, 0],\n",
       "       [8, 1, 2],\n",
       "       [8, 7, 0],\n",
       "       [1, 2, 2],\n",
       "       [9, 4, 2]])"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 166,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9],\n",
       "       [1, 1],\n",
       "       [8, 7],\n",
       "       [5, 6],\n",
       "       [5, 3],\n",
       "       [8, 8],\n",
       "       [8, 1],\n",
       "       [8, 7],\n",
       "       [1, 2],\n",
       "       [9, 4]])"
      ]
     },
     "execution_count": 166,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#假设这是10条样本数据，前两列是样本的两个特征，第3列是样本的分类标记，请分离出样本的特征和分类 \n",
    "#标记，分别存放在两个变量中，用 X_train 存放样本特征(红色部份), y_train 存放分类标记(绿色部份) 5分 \n",
    "X_train=Y[:,0:2]\n",
    "X_train\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1],\n",
       "       [0],\n",
       "       [0],\n",
       "       [0],\n",
       "       [1],\n",
       "       [0],\n",
       "       [2],\n",
       "       [0],\n",
       "       [2],\n",
       "       [2]])"
      ]
     },
     "execution_count": 167,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y_train=Y[:,2:3]\n",
    "Y_train"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 185,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9, 1],\n",
       "       [1, 1, 0],\n",
       "       [8, 7, 0],\n",
       "       [5, 6, 0],\n",
       "       [5, 3, 1],\n",
       "       [8, 8, 0],\n",
       "       [8, 1, 2],\n",
       "       [8, 7, 0],\n",
       "       [1, 2, 2],\n",
       "       [9, 4, 2]])"
      ]
     },
     "execution_count": 185,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 186,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[8, 1, 1, 2, 9, 4]"
      ]
     },
     "execution_count": 186,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#请用numpy的比较运算，通过 y_train 中的数据，分离出 X_train 中的3个分类，如下图 5分\n",
    "A=[]\n",
    "B=[]\n",
    "C=[]\n",
    "\n",
    "for i in range(10):\n",
    "    if Y_train[i][0]==2:\n",
    "        A.append(X_train[i][0])\n",
    "        A.append(X_train[i][1])\n",
    "    elif Y_train[i][0]==1:\n",
    "        B.append(X_train[i][0])\n",
    "        B.append(X_train[i][1])\n",
    "    else:\n",
    "        C.append(X_train[i][0])\n",
    "        C.append(X_train[i][1])\n",
    "#分类为2的\n",
    "A"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 187,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[6, 9, 5, 3]"
      ]
     },
     "execution_count": 187,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分类为1的\n",
    "B\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 188,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 1, 8, 7, 5, 6, 8, 8, 8, 7]"
      ]
     },
     "execution_count": 188,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#分类为0\n",
    "C"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "作业三： \n",
    "服务器日志数据分析 \n",
    "log.txt文件记录了某个项目中某个api的调用情况，采样时间为每分钟一次，包括调用次数、响应时 间等信息，大约18万条数据，请进行探索性数据分析 \n",
    "1. 请将数据导入pandas中，加上列名，如下图所示 \n",
    "各列对应含义如下 5分 \n",
    "列名 描述 \n",
    "id 自增字段 \n",
    "api api对应的url \n",
    "count 单位时间内被访问的次数 \n",
    "res_time_sum 响应时间总和(毫秒) \n",
    "res_time_min 最小响应时间 \n",
    "res_time_max 最大响应时间 \n",
    "res_time_avg 平均值 \n",
    "interval 采样间隔时间(秒) \n",
    "created_at 创建日志时间 \n",
    "2. 检测是否有重复值 5分 \n",
    "    api可以换成是别的值\n",
    "    df['api'].describe()\n",
    "3. 检测是否有异常值 5分 \n",
    "    #分析异常时段\n",
    "    df['2018-11-01'][['count']].boxplot(showmeans=True,meanline=True)\n",
    "    plt.show()\n",
    "    出现小圆圈的话会有异常值\n",
    "4. 分析api和interval这两列的数据是否对分析有用，如果无用，说明为什么后将这两列丢弃 5分 \n",
    "    无用，api接口/interrval是固定的，和数据没什么影响\n",
    "5. 使用created_at这一列的数据作为时间索引 10分 \n",
    "    因为这个是想要数据分析的count/tes_time_sum/res_time_min/res_time_max/res_time_avg的发生时间，并且很精确。不重复\n",
    "    而tes_time_sum/res_time_min/res_time_max/res_time_avg是要分析的时间，内容可能有异常值和重复值\n",
    "6. 分析api调用次数情况，例如，在一天中，哪些时间是访问高峰，哪些时间段访问比较,如图所示，从凌晨2点到11点访问少，业务高峰出在现下午两三点，晚上八九点 5分 \n",
    "    #切出一天的数据，绘制一天时候的接口调用情况\n",
    "    df['2018-11-01']['count'].plot()\n",
    "    plt.show()\n",
    "7. 分析一天中api响应时间，如下图所示，可以看到在业务高峰时间段，最大响应时间和平均响应时间都有所上升 5分 \n",
    "    #某一天的响应时间，平均响应时间\n",
    "    df['2018-11-08']['res_time_avg'].plot()\n",
    "    df['2018-11-08'][['res_time_avg','tes_time_sum','res_time_min','res_time_max']].plot()\n",
    "\n",
    "8. 分析连续的几天数据，可以发现，每天的业务高峰时段都比较相似 5分 \n",
    "    df['2018-11-08':'2018-11-10']['count'].plot()\n",
    "\n",
    "9. 分析周末访问量是否有增加。如下图，可以发现，周末的下午和晚上，比非周末访问量多一些 5分 \n",
    "\n",
    "    df.groupby(['weekend',df.index.hour])['count'].mean().unstack(level=0).plot()\n",
    "'''"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
